<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: ClubController.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_ClubController extends Core_Controller_Action_Standard
{
	protected $_data;
	protected $_active;
	protected  $_subject; 
	
	public function init()
	{
		if (!$this->isExists()){
			$this->_redirectCustom(array('route' => 'core_home'));
		}
		
		$this->view->object = $this->_getParam('object');
		$this->view->object_id = $this->_getParam('object_id');
		$this->view->active = $this->_active = $this->_getParam('active');
		$this->_subject = Engine_Api::_()->getItem($this->view->object, $this->view->object_id);
		$this->_data = array('object' => $this->view->object, 'object_id' => $this->view->object_id);
	}
	
	public function requireAuth()
	{
		$subject = $this->_subject;
		if ($subject->getType() == 'page'){
			$func = 'isTeamMember';
		}else{
			$func = 'isOwner';
		}
	
		return $subject->$func(Engine_Api::_()->user()->getViewer());
	}
	
	public function isExists()
	{
		return Engine_Api::_()->getDbTable('modules', 'core')->isModuleEnabled('like');
	}
	
	public function isEnabledLikeClub($subject)
	{
		return Engine_Api::_()->like()->isEnabledLikeClub($subject);
	}
	
	public function manageAction() 
	{
	  if ($this->_subject->getType() != 'page'){
  		if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
  			$this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
        return ;
  		}
  		
  	  if( !$this->_helper->requireUser()->isValid() || !$this->_helper->requireAuth()->setAuthParams('like', null, 'create')->checkRequire() ) {
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_You do not have enough right.');
        return ;
      }
	  }
    
		$this->view->link = $this->_active ? 'disable' : 'enable';
		
		if ($this->requireAuth()){
			$like_club = Engine_Api::_()->like()->manageLikeClub($this->_data, $this->_active, $this->_subject->getOwner()->getIdentity());
		}
		
		if ($like_club !== null){
			$this->view->html = $this->view->render('_composeLink.tpl');
		}else{
			$this->view->error = 1;
			$this->view->html = 'Error';
		}
	}

	public function promoteAction()
	{
    if ($this->_subject->getType() != 'page'){
      if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
        return ;
      }
      
      if( !$this->_helper->requireUser()->isValid() || !$this->_helper->requireAuth()->setAuthParams('like', null, 'create')->checkRequire() ) {
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_You do not have enough right.');
        return ;
      }
    }
    
		if (!$this->requireAuth()) {
			$this->view->error = 1;
			$this->view->message = Zend_Registry::get('Zend_Translate')->_("like_Like club is disabled.");
		}
	}

	public function likeBoxAction()
	{
    $this->_helper->layout->disableLayout();
    
    if ($this->_subject->getType() != 'page'){
      if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
        return ;
      }
    }

    if (!Engine_Api::_()->authorization()->isAllowed('like', null, 'create')){
      $this->view->error = 1;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
      return ;
    }

    $this->view->base_url = 'http://' . $_SERVER['HTTP_HOST'];
    $this->view->icon_url = Engine_Api::_()->like()->getLogo();

		$likes = Engine_Api::_()->like()->getLikes($this->_data);
    $likes->setItemCountPerPage(8);

		
		$this->view->likes = $likes;
		$this->view->subject = Engine_Api::_()->getItem($this->_data['object'], $this->_data['object_id']);
		$this->view->html = $this->view->render('_composeLikeBox.tpl');
	}

  public function likeButtonAction()
	{
    $layoutHelper = $this->_helper->layout;
    $layoutHelper->disableLayout();
    
    if ($this->_subject->getType() != 'page'){
      if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
        return ;
      }
    }

    if (!Engine_Api::_()->authorization()->isAllowed('like', null, 'create')){
      $this->view->error = 1;
      $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
      return ;
    }

    $this->view->base_url = $baseUrl = 'http://' . $_SERVER['HTTP_HOST'];
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
    $this->view->subject = $subject = Engine_Api::_()->getItem($this->_data['object'], $this->_data['object_id']);
    
    $like_club = Engine_Api::_()->like()->getLikeClub($subject);
		$params = array('club_id' => $like_club->getIdentity());

    $urls = array();
    $likes_array = array();
    $likes = Zend_Paginator::factory(array());
    $this->view->actionName = 'like';
    $this->view->is_liked = false;
    $this->view->viewer_url = '';

    if ($viewer->getIdentity()){
      $params['friend_id'] = $viewer->getIdentity();
      $likes = Engine_Api::_()->getDbTable('likes', 'like')->getLikes($params);
      $likes->setItemCountPerPage(2);
      if ($likes->getTotalItemCount() > 0){
        foreach($likes as $like){
          $urls[$like->user_id] = $baseUrl . $like->getHref();
          $likes_array[] = $like->toArray();
        }
      }
      if (!Engine_Api::_()->like()->isLike($subject)){
        $this->view->actionName = 'like';
        $this->view->is_liked = false;
      }
      else{
        $this->view->actionName = 'unlike';
        $likes_array[] = $viewer->toArray();
        $this->view->is_liked = true;
      }

      $this->view->viewer_url = $viewer->getHref();
    }

    $this->view->icon_url = Engine_Api::_()->like()->getLogo();
  
    $this->view->like_logo = $likes;
    $this->view->likes_json = json_encode($likes_array);
    $this->view->likes = $likes;

    $this->view->urls = $urls;
    $this->view->like_url = $baseUrl . $this->view->url(array('action' => 'like', 'object' => $subject->getType(), 'object_id' => $subject->getIdentity()), 'like_remote');
    $this->view->unlike_url = $baseUrl . $this->view->url(array('action' => 'unlike', 'object' => $subject->getType(), 'object_id' => $subject->getIdentity()), 'like_remote');

    $this->view->like_img = $baseUrl . $this->view->baseUrl() . '/application/modules/Like/externals/images/like_button.png';
    $this->view->unlike_img = $baseUrl . $this->view->baseUrl() . '/application/modules/Like/externals/images/unlike_button.png';

    $this->view->like_count = (int)Engine_Api::_()->like()->getLikeCount($subject);
    $return_url = $baseUrl . $this->view->url(array('action' => 'like', 'object' => $subject->getType(), 'object_id' => $subject->getIdentity()), 'like_login'); 
    $this->view->login_url = $baseUrl . $this->view->url(array(), 'like_login') . '?return_url=' . $return_url;
    $this->view->viewer_id = (int)$viewer->getIdentity();
		$this->view->html = $this->view->render('_composeLikeButton.tpl');
	}
	
  public function suggestAction()
  {
    if ($this->_subject->getType() != 'page'){
      if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR');
        return ;
      }
      
      if( !$this->_helper->requireUser()->isValid() || !$this->_helper->requireAuth()->setAuthParams('like', null, 'create')->checkRequire() ) {
        $this->view->error = 1;
        $this->view->html = Zend_Registry::get('Zend_Translate')->_('like_You do not have enough right.');
        return ;
      }
    }
    
    $user_ids = $this->_getParam('user_ids', array());
    $object = $this->_subject;
    
    $viewer = Engine_Api::_()->user()->getViewer();
    
    if (!$viewer->getIdentity() || !$object->getIdentity() || empty($user_ids)){
      return ;
    }

    $email_params = array(
      'user' => $viewer->getTitle(),
      'link' => $this->view->htmlLink("http://".$_SERVER['HTTP_HOST'].$object->getHref(), $object->getTitle())
    );

    foreach (Engine_Api::_()->getItemTable('user')->find($user_ids) as $user){
      if ($viewer->getIdentity() == $user->getIdentity()){
        continue;
      }
      
      Engine_Api::_()->getApi('mail', 'core')->sendSystem(
        $user,
        'like_suggest_'.$object->getType(),
        $email_params
      );

      Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification(
        $user,
        $viewer,
        $object,
        'like_suggest',
        array('label' => $object->getTitle())
      );
    }

  }
	
	public function sendUpdateAction()
	{
    if ($this->_subject->getType() != 'page'){
      if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
        $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => true,
          'parentRefresh' => false,
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('like_LIKE_ADMIN_TURN_OFF_ERROR'))
        ));
        return ;
      }
      
      if( !$this->_helper->requireUser()->isValid() || !$this->_helper->requireAuth()->setAuthParams('like', null, 'create')->checkRequire() ) {
        $this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => true,
          'parentRefresh' => false,
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('like_You do not have enough right.'))
        ));
        return ;
      }
    }
		  
		$this->view->messageForm = $messageForm = new Like_Form_Message();
		
		$messageForm->setAction($this->view->url(array('action' => 'send-update'), 'like_club'));
		$messageForm->getElement('object')->setValue($this->view->object);
		$messageForm->getElement('object_id')->setValue($this->view->object_id);
		
		$subject = $this->_subject;
		$viewer = Engine_Api::_()->user()->getViewer();
		
		$values = array();
		if (!$this->getRequest()->isPost()){
			return ;
		}else{
			if ($messageForm->isValid($this->getRequest()->getPost())){
				$values = $messageForm->getValues();
			}else{
				$this->_forward('success', 'utility', 'core', array(
          'smoothboxClose' => true,
          'parentRefresh' => false,
          'messages' => array(Zend_Registry::get('Zend_Translate')->_('like_Your message was not send.'))
        ));
				return ;
			}
		}
		
		$convoTable = Engine_Api::_()->getDbTable('conversations', 'messages');
		$likes = Engine_Api::_()->like()->getLikes($this->_data);
		$notificationTable = Engine_Api::_()->getDbTable('notifications', 'activity');
		
		$recipients = array();
		foreach ($likes as $like){
      if ($viewer->getIdentity() == $like->user_id){
        continue ;
      }
      
			$recipients[] = $like->user_id;
			$user = Engine_Api::_()->getItem('user', $like->user_id);
			$notificationTable->addNotification($user, $viewer, $subject, 'like_send_update');
		}
		
		if (!empty($recipients)){
			$conversation = $convoTable->send($viewer, $recipients, $values['subject'], $values['body']);
		}
    
    $this->_forward('success', 'utility', 'core', array(
      'smoothboxClose' => true,
      'parentRefresh' => false,
      'messages' => array(Zend_Registry::get('Zend_Translate')->_('like_Your message successfully sent.'))
    ));
	}
}